package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Dialect;
import com.example.demo.mapper.DialectMapper;
import com.example.demo.service.IDialectService;
import com.example.demo.service.PersonService;
import com.example.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author king
 * @since 2024-05-23
 */
@RequestMapping("/dialect")
@RestController
@Api(tags = "对话记录管理")
@CrossOrigin
public class DialectController {
    @Autowired
    private PersonService personService;
    @Autowired
    private IDialectService dialectService;
    @Autowired
    private DialectMapper dialectMapper;
    @ApiOperation("获取当前对话记录信息")
    @GetMapping("/getDialect")
    public IPage<Dialect> getDialect(@RequestParam Integer pageNo
            , @RequestParam Integer pageSize
//            , @RequestParam(defaultValue = "") String  userName
//            , @RequestParam(defaultValue = "") String  date
//            , @RequestParam(defaultValue = "") String  num
    ) throws Exception {

        Result result=new Result<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        IPage<Dialect> page=new Page<>(pageNo,pageSize);
        QueryWrapper<Dialect> dialectQueryWrapper=new QueryWrapper<>();
        dialectQueryWrapper.orderByDesc("id");
        List<Dialect> publications=dialectService.page(page,dialectQueryWrapper).getRecords();
        page.setTotal(publications.size());
//        res.put("data",data);
//        res.put("total",total);
//        result.setData(res);
//        System.out.println(res);
        System.out.println(dialectService.page(page,dialectQueryWrapper));
        return dialectService.page(page,dialectQueryWrapper) ;
    }
    @PostMapping("/getDialectId")
    @ApiOperation(value = "获取对话id")
    public Result getDialectId(@RequestParam String username,@RequestParam String flagnum) throws Exception {

Result result=new Result<>();

        LambdaQueryWrapper<Dialect>dialectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dialectLambdaQueryWrapper.eq(Dialect::getUsername,username);
        dialectLambdaQueryWrapper.eq(Dialect::getFlagnum,flagnum);
        Dialect dialect=dialectMapper.selectOne(dialectLambdaQueryWrapper);
        if(dialect!=null)
        {
            result.setMessage("成功拿到对话id");
            result.setResult(dialect);
            return result;
        }
        else {
            result.setMessage("没有找到该对话");
            result.setResult(null);
            return result;
        }
//        return dialectService.save(work);
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加对话记录信息")
    public Result add(@RequestBody Dialect dialect) throws Exception {
        Result result=new Result<>();

        LambdaQueryWrapper<Dialect>dialectLambdaQueryWrapper1=new LambdaQueryWrapper<>();
        dialectLambdaQueryWrapper1.eq(Dialect::getUsername,dialect.getUsername());
        dialectLambdaQueryWrapper1.eq(Dialect::getTitle,dialect.getTitle());
        dialectLambdaQueryWrapper1.eq(Dialect::getFlagnum,dialect.getFlagnum());
        dialectLambdaQueryWrapper1.eq(Dialect::getModule,dialect.getModule());
        Dialect dialect2=dialectMapper.selectOne(dialectLambdaQueryWrapper1);
        if(dialect2==null)
        {
            Boolean p=dialectService.save(dialect);
            if(p)
            {
                LambdaQueryWrapper<Dialect>dialectLambdaQueryWrapper=new LambdaQueryWrapper<>();
                dialectLambdaQueryWrapper.eq(Dialect::getUsername,dialect.getUsername());
                dialectLambdaQueryWrapper.eq(Dialect::getTitle,dialect.getTitle());
                dialectLambdaQueryWrapper.eq(Dialect::getFlagnum,dialect.getFlagnum());
                dialectLambdaQueryWrapper.eq(Dialect::getModule,dialect.getModule());
                Dialect dialect1=dialectMapper.selectOne(dialectLambdaQueryWrapper);
                result.setResult(dialect1);
                return result;
            }
            else
            {
                result.setResult(false);
                return result;
            }
        }
        else
        {
            result.setResult(false);
            result.setMessage("当前信息已经存在");
            return result;
        }


    }
    @PostMapping("/addAll")
    @ApiOperation(value = "添加对话记录完整信息")
    public Result add1(@RequestBody String params) throws Exception {
       Result result=new Result<>();

        return dialectService.add1(params);
    }
    @GetMapping("/getAll")
    @ApiOperation(value = "获取对话记录完整信息")
    public Result getAll(@RequestParam String username) throws Exception {
        Result result=new Result<>();
//        if(personService.checkToken(token).getResult()==null)
//        {
//            result.setMessage("token验证失败");
//            return result;
//        }
        return dialectService.getAll(username);
    }
    //
    //根据主键删除
    @GetMapping("/delete")
    @ApiOperation(value = "删除对话记录信息")
    public Result deleteById(@RequestParam String id) throws Exception {
        Result result=new Result<>();

        result.setResult(dialectService.removeById(id));
        return result;
    }
    @PostMapping("/deleteAll")
    @ApiOperation(value = "删除一群对话记录信息")
    public Result deleteAll(@RequestBody List<String> ids) throws Exception {
        Result result=new Result<>();

        result.setResult(dialectService.removeByIds(ids));
        return result;
    }
    //
    //根据姓名删除 构造条件即可
//    @GetMapping("/deleteByName")
//    @ApiOperation(value = "根据名字删除工作信息")
//    public Result deleteByName(@RequestParam String username)
//    {
//        Result result=new Result<>();
//        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
//        userQueryWrapper.eq("username",username);
//        User user=userservice.getOne(userQueryWrapper);
//
//        result.setData(userservice.removeById(user.getId()));
//        return result;
//    }
    @PostMapping("/get")
    @ApiOperation("获取对话记录信息")
    public Dialect get(@RequestBody Dialect work) throws Exception {

        return dialectService.getById(work.getId());
    }
    @PostMapping("/getByModule")
    @ApiOperation("通过module获取对话记录信息")
    public Result getByModule(@RequestParam String userName, @RequestParam String module) throws Exception {
        return dialectService.getAll1(userName,module);
    }
    //
    @PostMapping("/update")
    @ApiOperation("修改对话记录信息")
    public Result update(@RequestBody Dialect work) throws Exception {
        System.out.println(work);
        Result result=new Result<>();
        QueryWrapper<Dialect>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",work.getId());
        result.setResult(dialectService.update(work,queryWrapper));
        return result;
    }

}

